package com.jerry.utils;

import com.jerry.BO.FileChunkMeta;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class ChunkAddressStrategy {
    private final Map<String, ChunkAddressBuilder> builderMap;

    public ChunkAddressStrategy(List<ChunkAddressBuilder> builderList) {
        this.builderMap = new HashMap<>();
        builderList.forEach((builder) -> builderMap.put(builder.schema(), builder));
    }

    public String get(FileChunkMeta fileChunkMeta){
        String schema = fileChunkMeta.getSchema();
        ChunkAddressBuilder chunkAddressBuilder = builderMap.get(schema);
        if (chunkAddressBuilder == null){
            throw new RuntimeException("协议不支持");
        }
        return chunkAddressBuilder.build(fileChunkMeta);
    }
}
